import router from './router'
import { getToken } from '@/services/token'

const WHITE_NAME_LIST: string[] = ['login']

router.beforeEach(async (to, from, next) => {
  const hasToken = getToken()
  if (hasToken) {
    if (to.name === '/login') {
      next({ path: '/' })
    } else {
      next()
    }
  } else {
    if (WHITE_NAME_LIST.includes(to.name as string)) {
      next()
    } else {
      next(`/login?redirect=${to.path}`)
    }
  }
})

router.afterEach(() => {
  console.log('Go Done!')
})
